package com.cloud.wms.ui.returned.presenter;

import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.utils.MessageCreator;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.returned.contact.ReturnRegisterContact;
import com.cloud.wms.ui.returned.entity.ExpressDeliveryEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.functions.Function;

/**
 * Created by FC on 2021/5/13.
 * Desc：
 */
public class ReturnRegisterPresenter extends BaseRxPresenter<ReturnRegisterContact.View> implements ReturnRegisterContact.presenter {
    public static final int GET_EXPRESS_DELIVERY_MSG = 0x100;
    public static final int CHECK_TRACKING_NUMBER = 0x101;
    public static final int CONFIRM_RETURN_REGISTER = 0x102;

    @Inject
    public ReturnRegisterPresenter() {

    }

    /**
     * 获取所有快递公司
     */
    @Override
    public void getExpressDeliveryMsg() {
        Params params = Params.newInstance(Params.Type.MEMBER);
//        Observable.timer(500, TimeUnit.MILLISECONDS)
//                .map(new Function<Long, List<ExpressDeliveryEntity>>() {
//                    @Override
//                    public List<ExpressDeliveryEntity> apply(Long aLong) throws Exception {
//                        List<ExpressDeliveryEntity> entityList = new ArrayList<>();
//                        for (int i = 0; i < 5; i++) {
//                            ExpressDeliveryEntity deliveryEntity = new ExpressDeliveryEntity();
//                            deliveryEntity.setExpressDeliveryID(i + "");
//                            deliveryEntity.setExpressDeliveryName("顺丰" + i);
//                            entityList.add(deliveryEntity);
//                        }
//                        return entityList;
//                    }
//                })
        FineExApplication.component().http()
                .getExpressDeliveryMsg(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<List<ExpressDeliveryEntity>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(List<ExpressDeliveryEntity> expressDeliveryEntities) {
                        mView.onSuccess(MessageCreator.createMessage(expressDeliveryEntities, GET_EXPRESS_DELIVERY_MSG));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 检测快递单是否是原单
     *
     * @param trackingNumber    快递单号
     * @param expressDeliveryID 快递网点ID
     */
    @Override
    public void checkTrackingNumber(String trackingNumber, String expressDeliveryID) {
        Params params = Params.newInstance(Params.Type.MEMBER);
        params.put("expressDeliveryCode", trackingNumber);
        params.put("expressDeliveryID", expressDeliveryID);
//        Observable.timer(500, TimeUnit.MILLISECONDS)
//                .map(new Function<Long, ExpressDeliveryEntity>() {
//                    @Override
//                    public ExpressDeliveryEntity apply(Long aLong) throws Exception {
//                        ExpressDeliveryEntity deliveryEntity = new ExpressDeliveryEntity();
//                        deliveryEntity.setOriginalOrder(true);
//                        deliveryEntity.setMemberID("1");
//                        deliveryEntity.setMemberName("雪花秀");
//                        return deliveryEntity;
//                    }
//                })
        FineExApplication.component().http()
                .checkTrackingNumber(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<ExpressDeliveryEntity>(mView.provideContext()) {
                    @Override
                    public void onSuccess(ExpressDeliveryEntity expressDeliveryEntities) {
                        mView.onSuccess(MessageCreator.createMessage(expressDeliveryEntities, CHECK_TRACKING_NUMBER));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 确认登记
     *
     * @param expressDeliveryID
     * @param codeList
     */
    @Override
    public void confirmRegister(String expressDeliveryID, List<String> codeList) {
        Params params = Params.newInstance(Params.Type.MEMBER);
        params.put("userID", FineExApplication.component().loginCache().getUserInfo().getUserID());
        params.put("expressDeliveryID", expressDeliveryID);
        params.put("codeList", codeList);
//        Observable.timer(500, TimeUnit.MILLISECONDS)
//                .map(new Function<Long, Boolean>() {
//                    @Override
//                    public Boolean apply(Long aLong) throws Exception {
//                        return true;
//                    }
//                })
        FineExApplication.component().http()
                .confirmReturnRegister(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Object>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Object b) {
                        mView.onSuccess(MessageCreator.createMessage(b, CONFIRM_RETURN_REGISTER));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }
}
